<?php

declare(strict_types=1);

namespace App\Traits\Model\Admin;


use App\Model\Admin\Permission as PermissionModel;
use App\Request\FormRequest;
use function Hyperf\Support\make;

trait Menu
{
    /**
     * 设置应用
     * @param $value
     */
    public function setAppAttribute($value)
    {
        if (!$value) {
            $value = getApp();
        }
        $this->attributes['app'] = $value;
    }

    /**
     * 设置排序
     * @param null $value
     * @return mixed|null
     */
    public function setSortAttribute($value=null)
    {
        if ($value === null) {
            $value = self::getMaxSort($this->attributes['parent_id']);
            ++$value;
        }

        $this->attributes['sort'] = $value;
    }

    /**
     * 查询子集目录
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    /**
     * 获取权限
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function permission()
    {
        return $this->hasMany(PermissionModel::class);
    }
}
